package cn.iocoder.yudao.module.jikong.controller.admin.area;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.jikong.controller.admin.area.vo.*;
import cn.iocoder.yudao.module.jikong.dal.dataobject.area.JkAreaDO;
import cn.iocoder.yudao.module.jikong.service.area.JkAreaService;

@Tag(name = "管理后台 - 区域列表")
@RestController
@RequestMapping("/jikong/jk-area")
@Validated
public class JkAreaController {

    @Resource
    private JkAreaService jkAreaService;

    @PostMapping("/create")
    @Operation(summary = "创建区域列表")
    @PreAuthorize("@ss.hasPermission('jikong:area:create')")
    public CommonResult<Long> createJkArea(@Valid @RequestBody JkAreaSaveReqVO createReqVO) {
        return success(jkAreaService.createJkArea(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新区域列表")
    @PreAuthorize("@ss.hasPermission('jikong:area:update')")
    public CommonResult<Boolean> updateJkArea(@Valid @RequestBody JkAreaSaveReqVO updateReqVO) {
        jkAreaService.updateJkArea(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除区域列表")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('jikong:area:delete')")
    public CommonResult<Boolean> deleteJkArea(@RequestParam("id") Long id) {
        jkAreaService.deleteJkArea(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除区域列表")
                @PreAuthorize("@ss.hasPermission('jikong:area:delete')")
    public CommonResult<Boolean> deleteJkAreaList(@RequestParam("ids") List<Long> ids) {
        jkAreaService.deleteJkAreaListByIds(ids);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得区域列表")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('jikong:area:query')")
    public CommonResult<JkAreaRespVO> getJkArea(@RequestParam("id") Long id) {
        JkAreaDO jkArea = jkAreaService.getJkArea(id);
        return success(BeanUtils.toBean(jkArea, JkAreaRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得区域列表分页")
    @PreAuthorize("@ss.hasPermission('jikong:area:query')")
    public CommonResult<PageResult<JkAreaRespVO>> getJkAreaPage(@Valid JkAreaPageReqVO pageReqVO) {
        PageResult<JkAreaDO> pageResult = jkAreaService.getJkAreaPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, JkAreaRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出区域列表 Excel")
    @PreAuthorize("@ss.hasPermission('jikong:area:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportJkAreaExcel(@Valid JkAreaPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<JkAreaDO> list = jkAreaService.getJkAreaPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "区域列表.xls", "数据", JkAreaRespVO.class,
                        BeanUtils.toBean(list, JkAreaRespVO.class));
    }


    @GetMapping("/simple-list")
    @Operation(summary = "获得区域列表下拉框")
    public CommonResult<PageResult<JkAreaRespVO>> getJkAreaSimpleList(@Valid JkAreaPageReqVO pageReqVO) {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        PageResult<JkAreaDO> pageResult = jkAreaService.getJkAreaPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, JkAreaRespVO.class));
    }



}
